//
//  EditContactViewController.m
//  Edumap
//
//  Created by 辰领科技 on 16/7/20.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "EditContactViewController.h"
#import "HXProvincialCitiesCountiesPickerview.h"
@interface EditContactViewController ()
@property (nonatomic,strong) HXProvincialCitiesCountiesPickerview *regionPickerView;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroview;
@property (strong, nonatomic) IBOutlet UITextField *nameTextField;//联系人
@property (strong, nonatomic) IBOutlet UITextField *phoneNumberTextField;//手机号
@property (strong, nonatomic) IBOutlet UITextField *areaTextField;//所在区域
@property (strong, nonatomic) IBOutlet UITextField *addressTextField;//详细地址
@property (strong, nonatomic) IBOutlet UIButton *deleteButton;
@property (weak, nonatomic) IBOutlet UIButton *saveButton;

@property(nonatomic,strong)NSString * captionId;
@property(nonatomic,strong)NSString * cityId;
@property(nonatomic,strong)NSString * sectionId;
@property(nonatomic,strong)NSString * captionName;
@property(nonatomic,strong)NSString * cityName;
@property(nonatomic,strong)NSString * sectionName;
@end

@implementation EditContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewLayout];
    if (_isEditContact) {
        [self getAddressInfo];
    }
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroview];
    _scroview.contentSize = flexibleSize(CGSizeMake(0, 505), NO);
    
    self.deleteButton.layer.cornerRadius = 5*VerticalRatio();
    [_nameTextField setValue:[UIColor colorWithWhite:0.396 alpha:1.000]
               forKeyPath:@"_placeholderLabel.textColor"];
    [_phoneNumberTextField setValue:[UIColor colorWithWhite:0.396 alpha:1.000]
                  forKeyPath:@"_placeholderLabel.textColor"];
    [_areaTextField setValue:[UIColor colorWithWhite:0.396 alpha:1.000]
                  forKeyPath:@"_placeholderLabel.textColor"];
    [_addressTextField setValue:[UIColor colorWithWhite:0.396 alpha:1.000]
                         forKeyPath:@"_placeholderLabel.textColor"];
    
    if (_isEditContact) {
        _deleteButton.hidden = NO;
        _titleLabel.text = @"编辑联系人";
        [_saveButton setTitle:@"删除" forState:UIControlStateNormal];
    } else if (!_isEditContact) {
        _deleteButton.hidden = YES;
        _titleLabel.text = @"新建联系人";
    }

}



- (IBAction)back:(id)sender {

    [self backBtn];
}

#pragma mark == 获取地址详情
- (void)getAddressInfo{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_addressId forKey:@"msadId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:JY_queryMallShippingAddressById withHttpType:@"POST" withTag:JY_JY_queryMallShippingAddressById];
}

#pragma mark - 保存
- (IBAction)save:(id)sender {


    if (_isEditContact) {//编辑删除

        NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
        [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
        [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
        [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
        [dic setObject:_addressId forKey:@"ids"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:JY_deleteMallShippingAddress withHttpType:@"POST" withTag:JY_JY_deleteMallShippingAddress];

    } else if (!_isEditContact) {//新增保存
        if (_nameTextField.text.length==0) {
            [CWHUDTips showTips:_nameTextField.placeholder];
            return;
        }
        if (_addressTextField.text.length==0) {
            [CWHUDTips showTips:_addressTextField.placeholder];
            return;
        }
        if (_areaTextField.text.length==0) {
            [CWHUDTips showTips:_areaTextField.placeholder];
            return;
        }
        
        if (!_captionName||!_cityName||!_sectionName) {
            [CWHUDTips showTips:@"请选择所在区域"];
            return;
        }
        
        NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
        [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
        [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
        [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
        [dic setObject:self.nameTextField.text forKey:@"msadReceiverName"];
        [dic setObject:self.phoneNumberTextField.text forKey:@"msadMobileNo"];
        [dic setObject:self.addressTextField.text forKey:@"msadAddr"];
        [dic setObject:_captionId forKey:@"msadProvinceId"];
        [dic setObject:_cityId forKey:@"msadCityId"];
        [dic setObject:_sectionId forKey:@"msadAreaId"];
        [dic setObject:_captionName forKey:@"msadProvinceName"];
        [dic setObject:_cityName forKey:@"msadCityName"];
        [dic setObject:_sectionName forKey:@"msadAreaName"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:JY_addMallShippingAddress withHttpType:@"POST" withTag:JY_JY_addMallShippingAddress];

    }
}

#pragma mark - 编辑保存
- (IBAction)delete:(UIButton *)sender {
    if (_nameTextField.text.length==0) {
        [CWHUDTips showTips:_nameTextField.placeholder];
        return;
    }
    if (_addressTextField.text.length==0) {
        [CWHUDTips showTips:_addressTextField.placeholder];
        return;
    }
    if (_areaTextField.text.length==0) {
        [CWHUDTips showTips:_areaTextField.placeholder];
        return;
    }
    
    if (!_captionName||!_cityName||!_sectionName) {
        [CWHUDTips showTips:@"请选择所在区域"];
        return;
    }
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    [dic setObject:self.nameTextField.text forKey:@"msadReceiverName"];
    [dic setObject:self.phoneNumberTextField.text forKey:@"msadMobileNo"];
    [dic setObject:self.addressTextField.text forKey:@"msadAddr"];
    [dic setObject:_captionId forKey:@"msadProvinceId"];
    [dic setObject:_cityId forKey:@"msadCityId"];
    [dic setObject:_sectionId forKey:@"msadAreaId"];
    [dic setObject:_captionName forKey:@"msadProvinceName"];
    [dic setObject:_cityName forKey:@"msadCityName"];
    [dic setObject:_sectionName forKey:@"msadAreaName"];
    [dic setObject:_addressId forKey:@"msadId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:JY_updateMallShippingAddress withHttpType:@"POST" withTag:JY_JY_updateMallShippingAddress];
}

#pragma mark  ==== 编辑地址
- (IBAction)editAddress:(id)sender {
    [self hideKeyBoard];
    [self.regionPickerView showPickerWithProvinceName:_captionName cityName:_cityName countyName:_sectionName];
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==JY_JY_addMallShippingAddress) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            [self backBtn];
        }
    } else if (tag==JY_JY_updateMallShippingAddress) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            [self backBtn];
        }
    } else if (tag==JY_JY_deleteMallShippingAddress) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
             [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            [self backBtn];
        }
    }else if (tag == JY_JY_queryMallShippingAddressById){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
            NSDictionary * dic = database[@"result"];
            _phoneNumberTextField.text = [NSString stringWithFormat:@"%@",dic[@"msadMobileNo"]];
            _nameTextField.text = [NSString stringWithFormat:@"%@",dic[@"msadReceiverName"]];
            _areaTextField.text = [NSString stringWithFormat:@"%@%@%@",dic[@"msadProvinceName"],dic[@"msadCityName"],dic[@"msadAreaName"]];
            _addressTextField.text = [NSString stringWithFormat:@"%@",dic[@"msadAddr"]];
            _captionId = [NSString stringWithFormat:@"%@",dic[@"msadProvinceId"]];
            _cityId =  [NSString stringWithFormat:@"%@",dic[@"msadCityId"]];
            _sectionId = [NSString stringWithFormat:@"%@",dic[@"msadAreaId"]];
            _captionName = [NSString stringWithFormat:@"%@",dic[@"msadProvinceName"]];
            _cityName =  [NSString stringWithFormat:@"%@",dic[@"msadCityName"]];
            _sectionName = [NSString stringWithFormat:@"%@",dic[@"msadAreaName"]];
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}

- (HXProvincialCitiesCountiesPickerview *)regionPickerView {
    if (!_regionPickerView) {
        _regionPickerView = [[HXProvincialCitiesCountiesPickerview alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        
        __weak typeof(self) wself = self;
        _regionPickerView.completion = ^(CityArea *provinceArea,CityArea *cityArea,CityArea *countyArea) {
            __strong typeof(wself) self = wself;
        self.areaTextField.text = [NSString stringWithFormat:@"%@%@%@",!provinceArea?@"":provinceArea.a_name,!cityArea?@"":cityArea.a_name,!countyArea?@"":countyArea.a_name];
            _captionName = !provinceArea?nil:provinceArea.a_name;
            _captionId = [NSString stringWithFormat:@"%ld",!provinceArea?0:provinceArea.a_id];
            _cityName = !cityArea?nil:cityArea.a_name;
            _cityId = [NSString stringWithFormat:@"%ld",!cityArea?0:cityArea.a_id];
            _sectionName = !countyArea?nil:countyArea.a_name;
            _sectionId = [NSString stringWithFormat:@"%ld",!countyArea?0:countyArea.a_id];
        };
        [self.view addSubview:_regionPickerView];
    }
    return _regionPickerView;
}


@end
